package net.libra.ui
{
	import net.libra.core.interfaces.IDestroy;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	
	import net.libra.ui.base.BaseSprite;

	/**
	 * tab页
	 * @author elvisjiang
	 * 
	 */	
	public class TabNav extends EventDispatcher implements IDestroy
	{
		private var _skin:MovieClip;
		private var _btnGroup:ButtonGroup;
		private var _containerArray:Array;
		
		
		private var _containerChilds:Array;
		/**
		 * 
		 * @param btnArry 按钮数组
		 * @param containerArray 页面数组
		 * 
		 */		
		public function TabNav(skin:MovieClip,buttonGroup:ButtonGroup,containerArray:Array)
		{
			_skin = skin;
			_btnGroup = buttonGroup;
			_btnGroup.addEventListener( SelectEvent.SELECT,__select);
			_containerArray = containerArray;
			_currentIndex = 0;
			_containerChilds = [containerArray[0]];
			_skin.addChild(containerArray[0]);
		}
		
		private var _currentIndex:int;
		protected function __select(event:SelectEvent):void
		{
			_currentIndex = event.index;
			updateContainers();
		}
		
		private function updateContainers():void
		{
			_skin.removeChild(_containerChilds.shift());
			_skin.addChild(_containerArray[_currentIndex]);
			_containerChilds[_containerChilds.length] = _containerArray[_currentIndex];
		}

		public function get currentIndex():int
		{
			return _currentIndex;
		}
		
		public function set currentIndex(value:int):void
		{
			_currentIndex = value;
		}
		
		public function destroy():void
		{
			_btnGroup.removeEventListener( SelectEvent.SELECT,__select);
			_btnGroup.destroy();
			_containerArray.length = 0;
			_containerChilds.length = 0;
			_skin = null;
		}
		
	}
}